package cn.cloudplug.friendsrecommend.adapter;

import java.util.List;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.cloudplug.friendsrecommend.R;
import cn.cloudplug.friendsrecommend.entity.Race;
import cn.cloudplug.friendsrecommend.util.Constant;
import cn.cloudplug.friendsrecommend.util.GetPhotoUtil;
import cn.cloudplug.friendsrecommend.util.ShareUtil;

public class MyListAdapter extends BaseAdapter{

	private List<Race> races;
	private Activity activity;
	private LayoutInflater inflater;
	private Handler handler;

	public MyListAdapter(Activity activity, List<Race> races, Handler handler) {
		// TODO Auto-generated constructor stub
		this.activity = activity;
		this.races = races;
		inflater = LayoutInflater.from(activity);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return races.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View view, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder vh = null;
		if (view == null) {
			vh = new ViewHolder();
			view = inflater.inflate(R.layout.list_item_race, null);
			vh.iv_main_imageview = (ImageView) view.findViewById(R.id.iv_main_imageview);
			vh.iv_add_focus = (ImageView) view.findViewById(R.id.iv_add_focus);
			vh.iv_share = (ImageView) view.findViewById(R.id.iv_share);
			vh.tv_time_sign = (TextView) view.findViewById(R.id.tv_time_sign);
			vh.tv_start_date = (TextView) view.findViewById(R.id.tv_start_date);
			vh.tv_focus = (TextView) view.findViewById(R.id.tv_focus);
			view.setTag(vh);
		}
		vh = (ViewHolder) view.getTag();
		final Race r = races.get(arg0);
		vh.iv_main_imageview.setImageResource(r.getImageId());
		vh.tv_time_sign.setText("3天开始报名");
		vh.tv_start_date.setText("2015.05.05开赛");
		vh.tv_focus.setText(r.getFavorite() + "人关注");
		vh.iv_add_focus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!r.isIsfocus()) {
					Toast.makeText(activity, "关注成功", 0).show();
					r.setFavorite(r.getFavorite() + 1);
					r.setIsfocus(true);
					Message.obtain(handler, Constant.UPDATE_UI);
				}
				Toast.makeText(activity, "已经关注过了", 0).show();
			}
		});
		vh.iv_share.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ShareUtil share = new ShareUtil(activity);
				share.share("");
			}
		});
		return view;
	}

	class ViewHolder {
		TextView tv_time_sign;
		TextView tv_focus;
		TextView tv_start_date;
		ImageView iv_add_focus;
		ImageView iv_main_imageview;
		ImageView iv_share;
	}

	
}
